require "Net/send_Msg"
require 'View/UIBase/UIBase'

UIHeroCombination = UIBase.New(PanelNames.UIHeroCombination);
local data = nil;
function UIHeroCombination.Init(info)
    data = info;
    UIBase.Init(UIHeroCombination);
end

--定义控件
function UIHeroCombination:OnCreate()
    self.comList = self:GetChild("CombinationList");
    self.comList.itemRenderer = function(idx, obj)
        self:rendererItem(idx + 1, obj)
    end
    self.closeBtn = self:GetChild("bgClose");
end

function UIHeroCombination:OnEnable()
    self:addEvent()
    local count = table.getn(data);
    self.comList.height = self.comList:AddItemFromPool().height * count + math.floor(count / 2);
    self.comList.numItems = count;

end

function UIHeroCombination:OnDisable()

end

function UIHeroCombination:OnDestroy()

end

function UIHeroCombination:addEvent()
    self:RegisterOnClick(self.closeBtn, self.onCloseClick, self);

end

function UIHeroCombination:removeEvent()

end

function UIHeroCombination:onCloseClick()
    closeUI(PanelNames.UIHeroCombination);
end

function UIHeroCombination:rendererItem(idx, obj)
    local heroData = data[idx];
    local heroList = obj:GetChild("hero");
    local ctrl = obj:GetController("state");
    local cfg = HeroManager.getCombinationInfo()[tostring(heroData.skillId)]
    if heroData.type == 0 then
        ctrl.selectedIndex = 0;
        heroList.itemRenderer = function(idx, obj)
            self:rendererHero(idx + 1, obj, heroData);
        end
        heroList.numItems = table.getn(heroData.heroIds);
    else
        ctrl.selectedIndex = 1;
        local desText2 = obj:GetChild("Describe2");
        desText2.text = getLanguage(heroData.decs2);
    end
    local desText = obj:GetChild("Describe");
    desText.text = getLanguage(heroData.decs);

    local skillNameText = obj:GetChild("title");
    skillNameText.text = getLanguage(cfg.f_CombinationName);

    local iconImg = obj:GetChild("icon");
    iconImg.url = "ui://SkillAtlas/" .. cfg.f_CombinationIcon;
end

function UIHeroCombination:rendererHero(idx, obj, info)
    local charactor = CtrlManager.GetCtrl(PanelNames.HeadRectItem);
    --local ctrl = itemobj:GetTransition("Effect_List")
    --ctrl:Play()
    local hero = info.heroIds[idx];
    if charactor ~= nil then
        --初始英雄信息
        local heroData = {};
        logicMgr.HeroManager.copyHeroConfigbyModelId(hero, heroData);

        if heroData then
            charactor.initData(obj, heroData, nil, nil, nil, nil, nil, nil, true);
        end
    end
    obj.touchable = false;
end

return UIHeroCombination;